﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Meqs.Entity;
using Meqs.Logic.Interface;

public partial class StudentGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    //初始化 repeat控件，集成显示学生关联信息

    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind(1);
    }

    private void Bind(int pageIndex)
    {
        string strClassID = HFClassIDValue.Value;

        Hashtable table = new Hashtable();
        table.Add("SchoolMark", Common.GetConstSchoolMark());
        //table.Add("IsEffect", true);
        table.Add("ClsID", strClassID);

        IStudentInfoLogic studentLogic = Container<IStudentInfoLogic>.Instance;
        List<StudentInfo> list = (List<StudentInfo>)studentLogic.FindAll1(table);
        List<VStudentInfo> viewData = new List<VStudentInfo>();

        viewData.Clear();
        foreach (StudentInfo studentInfo in list)
        {
            string sex = studentInfo.Sexy == 0 ? "男" : "女";

            string sBirthDay = "";
            string sInSchoolDate = "";
            if (studentInfo.Birthday.Year != DateTime.MaxValue.Year)
                sBirthDay = studentInfo.Birthday.ToShortDateString();
            if (studentInfo.InSchoolDate.Year != DateTime.MaxValue.Year)
                sInSchoolDate = studentInfo.InSchoolDate.ToShortDateString();

            VStudentInfo vStudentInfo = new VStudentInfo(studentInfo.RID, studentInfo.StudentCode, studentInfo.StudentName, studentInfo.Passwd,
                                                         studentInfo.Sexy, studentInfo.Birthday, studentInfo.InSchoolDate, studentInfo.ClsID,
                                                         studentInfo.intIsEffect, studentInfo.SchoolMark,
                                                         sex, sBirthDay, sInSchoolDate);
            viewData.Add(vStudentInfo);
        }

        PagerBind(viewData, pageIndex);

    }

    //分页处理
    private void PagerBind(List<VStudentInfo> srcList, int pageIndex)
    {
        AspNetPager1.RecordCount = srcList.Count;
        AspNetPager1.CurrentPageIndex = pageIndex;

        int index = (pageIndex - 1) * AspNetPager1.PageSize;
        int count = 0;
        if (srcList.Count - index >= AspNetPager1.PageSize) count = AspNetPager1.PageSize;
        else count = srcList.Count - index;

        List<VStudentInfo> pageList = srcList.GetRange(index, count);

        Repeater1.DataSource = pageList;
        Repeater1.DataBind();
    } 

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        Wuqi.Webdiyer.PageChangedEventArgs pc = e as Wuqi.Webdiyer.PageChangedEventArgs;

        Bind(pc.NewPageIndex);
    }
    protected string GetStatusStr(int status)
    {
        string restr;
        switch (status)
        {
            case 0:
                //restr = "删除";
                //break;
            case -2:
                restr = "转学";
                break;
            case -1:
                restr = "休学";
                break;
            
                
            case 1:
                restr = "在校";
                break;
            default:
                restr = "未知";
                break;
        }
        return restr;
    }
    ////根据班级ID查询学生
    //[System.Web.Services.WebMethod]
    //public static int BindDataByClassNo(string strClassNo)
    //{
    //    if (String.IsNullOrEmpty(strClassNo))
    //        return -1;

    //    int classNo = int.Parse(strClassNo);
    //    Hashtable table = new Hashtable();
    //    table.Add("SchoolMark", Common.GetConstSchoolMark());
    //    table.Add("IsEffect", true);
    //    table.Add("ClsID", classNo);

    //    IStudentInfoLogic studentLogic = Container<IStudentInfoLogic>.Instance;
    //    List<StudentInfo> list = (List<StudentInfo>)studentLogic.FindAll(table);

    //    viewData.Clear();
    //    foreach (StudentInfo studentInfo in list)
    //    {
    //        string sex = studentInfo.Sexy == 0 ? "男" : "女";

    //        string sBirthDay = "";
    //        string sInSchoolDate = "";
    //        if (studentInfo.Birthday.Year != 1)
    //            sBirthDay = studentInfo.Birthday.ToShortDateString();
    //        if (studentInfo.InSchoolDate.Year != 1)
    //            sInSchoolDate = studentInfo.InSchoolDate.ToShortDateString();

    //        VStudentInfo vStudentInfo = new VStudentInfo(studentInfo.RID, studentInfo.StudentCode, studentInfo.StudentName, studentInfo.Passwd,
    //                                                     studentInfo.Sexy, studentInfo.Birthday, studentInfo.InSchoolDate, studentInfo.ClsID,
    //                                                     studentInfo.IsEffect, studentInfo.SchoolMark,
    //                                                     sex, sBirthDay, sInSchoolDate);
    //        viewData.Add(vStudentInfo);
    //    }

    //    return 0;
    //}

    //检查学生学号是否唯一
    [System.Web.Services.WebMethod]
    public static int ValidateUsed(int StudentId, string StudentCode)
    {
        int result = 0;
        IStudentInfoLogic studentLogic = Container<IStudentInfoLogic>.Instance;

        //1.新增
        if (StudentId == 0)
        {
            Hashtable tableNo = new Hashtable();
            tableNo.Add("SchoolMark", Common.GetConstSchoolMark());
            tableNo.Add("StudentCode", StudentCode);
            tableNo.Add("IsEffect", 1);

            result = studentLogic.FindCount(tableNo);
            //学生学号重复
            if (result > 0)
                return 1;

            if (tableNo.ContainsKey("IsEffect"))
                tableNo["IsEffect"] = 0;

            result = studentLogic.FindCount(tableNo);
            //学生学号重复
            if (result > 0)
                return 2;
        }
        //2.修改
        else
        {
            StudentInfo stuInfo = studentLogic.FindById(StudentId);
            
            //修改了学生学号

            if (stuInfo.StudentCode.ToUpper() != StudentCode.ToUpper())
            {
                Hashtable tableNo = new Hashtable();
                tableNo.Add("SchoolMark", Common.GetConstSchoolMark());
                tableNo.Add("StudentCode", StudentCode);
                tableNo.Add("IsEffect", 1);

                result = studentLogic.FindCount(tableNo);

                //学生学号在有效数据里面重复
                if (result > 0)
                    return 1;

                if (tableNo.ContainsKey("IsEffect"))
                    tableNo["IsEffect"] = 0;
                result = studentLogic.FindCount(tableNo);
                //学生学号在无效数据里面重复
                if (result > 0)
                    return 2;

            }
        }

        return result;
    }

    //获取学生信息
    [System.Web.Services.WebMethod]
    public static StudentInfo GetStudentInfoById(int studentID)
    {
        StudentInfo studentInfo = Container<IStudentInfoLogic>.Instance.FindById(studentID);
        return studentInfo;        
    }

    //添加学生信息
    [System.Web.Services.WebMethod]
    public static int InsertStudentInfo(StudentInfo studentInfo)
    {
        studentInfo.SchoolMark = Common.GetConstSchoolMark();
        studentInfo.IsEffect = true;
        studentInfo.Passwd = Common.GetStartPwd();

        if (studentInfo.Birthday == DateTime.MinValue)
            studentInfo.Birthday = DateTime.MaxValue;

        if(studentInfo.InSchoolDate == DateTime.MinValue)
            studentInfo.InSchoolDate = DateTime.MaxValue;

        IStudentInfoLogic studentInfoLogic = Container<IStudentInfoLogic>.Instance;
        int iResult = studentInfoLogic.Create(studentInfo);
        if (iResult > 0)
        {
            StudentHistory sh = new StudentHistory();
            SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
            ClsInfo ci = Container<IClsInfoLogic>.Instance.FindById(studentInfo.ClsID);
            sh.SchoolYear = si.SchoolYear;
            sh.Term = si.SchoolTerm;
            sh.Grade = ci.Grade;
            sh.ClassID = studentInfo.ClsID;
            sh.StudentID = iResult;
            Container<IStudentHistoryLogic>.Instance.Create(sh);
        }
        return iResult;
    }

    //更新学生信息
    [System.Web.Services.WebMethod]
    public static int UpdateStudentInfo(StudentInfo studentInfo)
    {
        studentInfo.SchoolMark = Common.GetConstSchoolMark();
        studentInfo.IsEffect = true;

        if (studentInfo.Birthday == DateTime.MinValue)
            studentInfo.Birthday = DateTime.MaxValue;

        if (studentInfo.InSchoolDate == DateTime.MinValue)
            studentInfo.InSchoolDate = DateTime.MaxValue;

        StudentInfo student = Container<IStudentInfoLogic>.Instance.FindById(studentInfo.RID);

        if (student != null)
        {
            studentInfo.Passwd = student.Passwd;

            int iResult = Container<IStudentInfoLogic>.Instance.Update(studentInfo);
            return iResult;
        }
        else
            return -1;
        
    }

    //删除多条学生信息
    [System.Web.Services.WebMethod]
    public static int DeleteStudentInfo(List<int> studentInfoList, int Status)
    {
        //int iResult = Container<IStudentInfoLogic>.Instance.DeleteStudentInfo(studentInfoList);
        //return iResult;
        int iResult = Container<IStudentInfoLogic>.Instance.ChangeStudentStatus(studentInfoList, Status);
        return iResult;
    }

    /// <summary>
    /// 批量设置教师密码
    /// </summary>
    /// <param name="listUserId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int ResetPassword(List<int> listUserId)
    {
        string strStartPwd = Common.GetStartPwd();

        int n = Container<IStudentInfoLogic>.Instance.ResetPassword(strStartPwd,listUserId);
        return n;
    }

    //初始化学生转班列表

    [System.Web.Services.WebMethod]
    public static List<StudentInfo> InitShiftClassList(List<int> listUserId)
    {
        List<StudentInfo> resultList = new List<StudentInfo>();
        IStudentInfoLogic studentInfoLogic = Container<IStudentInfoLogic>.Instance;

        foreach (int userId in listUserId)
        {
            StudentInfo studentInfo = studentInfoLogic.FindById(userId);
            resultList.Add(studentInfo);
        }

        return resultList;
    }

    //获取指定学校的所有班级

    [System.Web.Services.WebMethod]
    public static List<ClsInfo> GetClsInfoList()
    {
        IClsInfoLogic clsInfoLogic = Container<IClsInfoLogic>.Instance;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("IsEffect", true);

        return (List<ClsInfo>)clsInfoLogic.FindAll(ht);
    }

    //学生转班
    [System.Web.Services.WebMethod]
    public static int StudentShiftClass(List<int> listUserId, int newClsID)
    {
        IStudentInfoLogic studentInfoLogic = Container<IStudentInfoLogic>.Instance;
        ClsInfo newci = Container<IClsInfoLogic>.Instance.FindById(newClsID);
        
        foreach (int userId in listUserId)
        {
            StudentInfo studentInfo = studentInfoLogic.FindById(userId);
            Hashtable ht = new Hashtable();
            ht.Add("Grade", newci.Grade);
            ht.Add("NewClassID", newClsID);
            ht.Add("StudentID", studentInfo.RID);
            ht.Add("ClassID", studentInfo.ClsID);
            Container<IStudentHistoryLogic>.Instance.UpdateStudentClass(ht);
            studentInfo.ClsID = newClsID;
            studentInfoLogic.Update(studentInfo);
            
        }

        return 1;
    }
}
